package com.yaodian100.ec.generatepage.web;

import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.imageio.ImageIO;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.ec.category.entity.Category;
import com.yaodian100.ec.category.service.CategoryService;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.generate.entity.EventImage;
import com.yaodian100.ec.generate.entity.GenerateAdvertise;
import com.yaodian100.ec.generate.entity.GenerateContent;
import com.yaodian100.ec.generate.entity.GeneratePosition;
import com.yaodian100.ec.generate.service.GenerateService;

public class HotSelectAdvAction extends ActionSupport implements Preparable {

	private static final String FIELD_NAME_TAB1_PROD_ID = "tab1ProdId";
	private static final String FIELD_NAME_TAB2_PROD_ID = "tab2ProdId";
	private static final String FIELD_NAME_TAB3_PROD_ID = "tab3ProdId";
	private static final String FIELD_NAME_TAB4_PROD_ID = "tab4ProdId";
	// private static final String FIELD_NAME_TAB5_PROD_ID = "tab5ProdId";

	/**
	 * 
	 */
	private static final long serialVersionUID = 8280417163118221483L;

	private final Logger logger = LoggerFactory.getLogger(HotSelectAdvAction.class);
	private GenerateService generateService;
	private CategoryService categoryService;
	private GenerateAdvertise advertise;
	private GeneratePosition position;
	private GenerateContent content;
	private StringBuilder contents;
	private String advId;
	private String advName;
	private String advType = "000_HOTSELECT";
	private String advStatus;
	private String startDate;
	private String endDate;
	private String startTime;
	private String endTime;
	private Date createDate;
	private String createUser;
	private Date modifyDate;
	private String modifyUser;
	private String mainCateId;
	private List<Category> categoryRoot;

	private File tab1Img = null;
	private String tab1ImgName;
	private String tab1ImgFolder;
	private String tab1ProdId;
	private String tab1Brief;
	private String tab1Name;
	private String tab1TagType;
	private String tab1TagName;
	private File tab2Img = null;
	private String tab2ImgName;
	private String tab2ImgFolder;
	private String tab2ProdId;
	private String tab2Brief;
	private String tab2Name;
	private String tab2TagType;
	private String tab2TagName;
	private File tab3Img = null;
	private String tab3ImgName;
	private String tab3ImgFolder;
	private String tab3ProdId;
	private String tab3Brief;
	private String tab3Name;
	private String tab3TagType;
	private String tab3TagName;
	private File tab4Img = null;
	private String tab4ImgName;
	private String tab4ImgFolder;
	private String tab4ProdId;
	private String tab4Brief;
	private String tab4Name;
	private String tab4TagType;
	private String tab4TagName;
	// private File tab5Img = null;
	// private String tab5ImgName;
	// private String tab5ImgFolder;
	// private String tab5ProdId;
	// private String tab5Brief;
	// private String tab5Name;
	// private String tab5TagType;
	// private String tab5TagName;

	private EventImage eventImage = null;
	private String error;
	private final String lineBreak = "\n";

	/**
	 * 新增热搜精选
	 * 
	 * @return
	 */
	public String addHotSelectAdv() {
		advId = "";
		return "editHotSelectAdv";
	}

	/**
	 * 编辑热搜精选
	 * 
	 * @return
	 * @throws Exception
	 */
	public String editHotSelectAdv() throws Exception {
		initVariables();
		advertise = generateService.get(Long.parseLong(advId));
		position = generateService.getGeneratePositionByAdvId(Long.parseLong(advId)).get(0);
		content = generateService.getGenerateContentByAdvId(Long.parseLong(advId)).get(0);
		advStatus = advertise.getAdvStatus();
		advType = advertise.getAdvType();
		advName = advertise.getAdvName();
		startDate = FastDateFormat.getInstance(EcConstant.PATTERN_DATE).format(advertise.getStartDate());
		endDate = FastDateFormat.getInstance(EcConstant.PATTERN_DATE).format(advertise.getEndDate());
		DateFormat dateFormat = new SimpleDateFormat("HH:mm");
		startTime = dateFormat.format(advertise.getStartDate());
		endTime = dateFormat.format(advertise.getEndDate());
		createDate = advertise.getCreateDate();
		createUser = advertise.getCreateUser();
		modifyDate = advertise.getModifyDate();
		modifyUser = advertise.getModifyUser();
		mainCateId = position.getMainCateId().toString();
		readPropertise(content);
		return "editHotSelectAdv";
	}

	/**
	 * @return the advId
	 */
	public String getAdvId() {
		return advId;
	}

	/**
	 * @return the advName
	 */
	public String getAdvName() {
		return advName;
	}

	/**
	 * @return the advStatus
	 */
	public String getAdvStatus() {
		return advStatus;
	}

	/**
	 * @return the advType
	 */
	public String getAdvType() {
		return advType;
	}

	public List<Category> getCategoryRoot() {
		return categoryRoot;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	/**
	 * @return the content
	 */
	public GenerateContent getContent() {
		return content;
	}

	/**
	 * @return the endDate
	 */
	public String getEndDate() {
		return endDate;
	}

	public String getEndTime() {
		return endTime;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public String getCreateUser() {
		return createUser;
	}

	public Date getModifyDate() {
		return modifyDate;
	}

	public String getModifyUser() {
		return modifyUser;
	}

	public String getError() {
		return error;
	}

	public String getMainCateId() {
		return mainCateId;
	}

	public GeneratePosition getPosition() {
		return position;
	}

	/**
	 * @return the startDate
	 */
	public String getStartDate() {
		return startDate;
	}

	public String getStartTime() {
		return startTime;
	}

	public String getTab1Brief() {
		return tab1Brief;
	}

	/**
	 * @return the tab1Img
	 */
	public File getTab1Img() {
		return tab1Img;
	}

	/**
	 * @return the tab1ImgFolder
	 */
	public String getTab1ImgFolder() {
		return tab1ImgFolder;
	}

	/**
	 * @return the tab1ImgName
	 */
	public String getTab1ImgName() {
		return tab1ImgName;
	}

	public String getTab1Name() {
		return tab1Name;
	}

	public String getTab1ProdId() {
		return tab1ProdId;
	}

	public String getTab2Brief() {
		return tab2Brief;
	}

	/**
	 * @return the tab2Img
	 */
	public File getTab2Img() {
		return tab2Img;
	}

	/**
	 * @return the tab2ImgFolder
	 */
	public String getTab2ImgFolder() {
		return tab2ImgFolder;
	}

	/**
	 * @return the tab2ImgName
	 */
	public String getTab2ImgName() {
		return tab2ImgName;
	}

	public String getTab2Name() {
		return tab2Name;
	}

	public String getTab2ProdId() {
		return tab2ProdId;
	}

	public String getTab3Brief() {
		return tab3Brief;
	}

	/**
	 * @return the tab3Img
	 */
	public File getTab3Img() {
		return tab3Img;
	}

	/**
	 * @return the tab3ImgFolder
	 */
	public String getTab3ImgFolder() {
		return tab3ImgFolder;
	}

	/**
	 * @return the tab3ImgName
	 */
	public String getTab3ImgName() {
		return tab3ImgName;
	}

	public String getTab3Name() {
		return tab3Name;
	}

	public String getTab3ProdId() {
		return tab3ProdId;
	}

	public String getTab4Brief() {
		return tab4Brief;
	}

	/**
	 * @return the tab4Img
	 */
	public File getTab4Img() {
		return tab4Img;
	}

	/**
	 * @return the tab4ImgFolder
	 */
	public String getTab4ImgFolder() {
		return tab4ImgFolder;
	}

	/**
	 * @return the tab4ImgName
	 */
	public String getTab4ImgName() {
		return tab4ImgName;
	}

	public String getTab4Name() {
		return tab4Name;
	}

	public String getTab4ProdId() {
		return tab4ProdId;
	}

	// public String getTab5Brief() {
	// return tab5Brief;
	// }
	//
	// /**
	// * @return the tab5Img
	// */
	// public File getTab5Img() {
	// return tab5Img;
	// }
	//
	// /**
	// * @return the tab5ImgFolder
	// */
	// public String getTab5ImgFolder() {
	// return tab5ImgFolder;
	// }

	/**
	 * @return the tab5ImgName
	 */
	// public String getTab5ImgName() {
	// return tab5ImgName;
	// }
	//
	// public String getTab5Name() {
	// return tab5Name;
	// }

	// public String getTab5ProdId() {
	// return tab5ProdId;
	// }

	private boolean hotSelectAdvValidate() throws CoreException, ParseException, IOException {
		/**************************** 必填栏位 ****************************/
		logger.info("advName = " + advName);

		if (StringUtils.isBlank(advName))
			addFieldError("advName", "请输入广告名称");

		if (StringUtils.isBlank(startDate))
			addFieldError("startDate", "请输入起始日期");

		if (StringUtils.isBlank(endDate))
			addFieldError("endDate", "请输入结束日期");

		if (StringUtils.isBlank(mainCateId))
			addFieldError("mainCateId", "请选择馆别");

		if (StringUtils.isBlank(tab1ImgName))
			addFieldError("tab1ImgName", "請輸入tab1 img name");

		if (StringUtils.isBlank(tab2ImgName))
			addFieldError("tab2ImgName", "請輸入tab2 img name");

		if (StringUtils.isBlank(tab3ImgName))
			addFieldError("tab3ImgName", "請輸入tab3 img name");

		if (StringUtils.isBlank(tab4ImgName))
			addFieldError("tab4ImgName", "請輸入tab4 img name");

		// if (StringUtils.isBlank(tab5ImgName))
		// addFieldError("tab5ImgName", "請輸入tab5 img name");

		// 1
		if (StringUtils.isBlank(tab1ProdId))
			addFieldError(FIELD_NAME_TAB1_PROD_ID, "请输入商品1编号");
		else if (!generateService.isProductExist(tab1ProdId))
			addFieldError(FIELD_NAME_TAB1_PROD_ID, "商品1可能不存在，或是已被删除，或是不在架上");

		// 2
		if (StringUtils.isBlank(tab2ProdId))
			addFieldError(FIELD_NAME_TAB2_PROD_ID, "请输入商品2编号");
		else if (!generateService.isProductExist(tab2ProdId))
			addFieldError(FIELD_NAME_TAB2_PROD_ID, "商品2可能不存在，或是已被删除，或是不在架上");

		// 3
		if (StringUtils.isBlank(tab3ProdId))
			addFieldError(FIELD_NAME_TAB3_PROD_ID, "请输入商品3编号");
		else if (!generateService.isProductExist(tab3ProdId))
			addFieldError(FIELD_NAME_TAB3_PROD_ID, "商品3可能不存在，或是已被删除，或是不在架上");

		// 4
		if (StringUtils.isBlank(tab4ProdId))
			addFieldError(FIELD_NAME_TAB4_PROD_ID, "请输入商品4编号");
		else if (!generateService.isProductExist(tab4ProdId))
			addFieldError(FIELD_NAME_TAB4_PROD_ID, "商品4可能不存在，或是已被删除，或是不在架上");

		// 5
		// if (StringUtils.isBlank(tab5ProdId))
		// addFieldError(FIELD_NAME_TAB5_PROD_ID, "请输入商品5编号");
		// else if (!generateService.isProductExist(tab5ProdId))
		// addFieldError(FIELD_NAME_TAB5_PROD_ID, "商品5可能不存在，或是已被删除，或是不在架上");

		if (getFieldErrors().isEmpty())
			return true;
		else
			return false;
	}

	/**
	 * 初始化变数值
	 */
	void initVariables() {
		advertise = new GenerateAdvertise();
		position = new GeneratePosition();
		content = new GenerateContent();

		tab1Img = null;
		tab1ImgName = "";
		tab1ImgFolder = "";
		tab1ProdId = "";
		tab1Brief = "";
		tab1Name = "";
		tab1TagType = "";
		tab1TagName = "";

		tab2Img = null;
		tab2ImgName = "";
		tab2ImgFolder = "";
		tab2ProdId = "";
		tab2Brief = "";
		tab2Name = "";
		tab2TagType = "";
		tab2TagName = "";

		tab3Img = null;
		tab3ImgName = "";
		tab3ImgFolder = "";
		tab3ProdId = "";
		tab3Brief = "";
		tab3Name = "";
		tab3TagType = "";
		tab3TagName = "";

		tab4Img = null;
		tab4ImgName = "";
		tab4ImgFolder = "";
		tab4ProdId = "";
		tab4Brief = "";
		tab4Name = "";
		tab4TagType = "";
		tab4TagName = "";

		// tab5Img = null;
		// tab5ImgName = "";
		// tab5ImgFolder = "";
		// tab5ProdId = "";
		// tab5Brief = "";
		// tab5Name = "";
		// tab5TagType = "";
		// tab5TagName = "";

		startDate = "";
		endDate = "";
		startTime = "00:00";
		endTime = "00:00";
		createDate = null;
		createUser = "";
		modifyDate = null;
		modifyUser = "";
	}

	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		clearErrorsAndMessages();

		if (null == categoryRoot)
			categoryRoot = categoryService.getChild(0l);
	}

	/**
	 * 從db讀取propertise資料
	 * 
	 * @param content
	 * @throws IOException
	 */
	void readPropertise(GenerateContent content) throws IOException {
		Properties prop = new Properties();
		Reader reader = new StringReader(content.getContents());
		prop.load(reader);
		reader.close();
		tab1ImgName = StringUtils.defaultString(prop.get("tab1ImgName").toString());
		// System.out.println("tab1ImgName/" + tab1ImgName);
		tab1ImgFolder = StringUtils.defaultString(prop.get("tab1ImgFolder").toString(), "");
		tab1ProdId = StringUtils.defaultString(prop.get("tab1ProdId").toString(), "");
		tab1Brief = StringUtils.defaultString(prop.get("tab1Brief").toString(), "");
		tab1Name = StringUtils.defaultString(prop.get("tab1Name").toString(), "");
		// 為了舊資料
		if (prop.containsKey("tab1TagType") && prop.containsKey("tab1TagName")) {
			tab1TagType = StringUtils.defaultString(prop.get("tab1TagType").toString(), "");
			tab1TagName = StringUtils.defaultString(prop.get("tab1TagName").toString(), "");
		}

		tab2ImgName = StringUtils.defaultString(prop.get("tab2ImgName").toString());
		tab2ImgFolder = StringUtils.defaultString(prop.get("tab2ImgFolder").toString(), "");
		tab2ProdId = StringUtils.defaultString(prop.get("tab2ProdId").toString(), "");
		tab2Brief = StringUtils.defaultString(prop.get("tab2Brief").toString(), "");
		tab2Name = StringUtils.defaultString(prop.get("tab2Name").toString(), "");
		// 為了舊資料
		if (prop.containsKey("tab2TagType") && prop.containsKey("tab2TagName")) {
			tab2TagType = StringUtils.defaultString(prop.get("tab2TagType").toString(), "");
			tab2TagName = StringUtils.defaultString(prop.get("tab2TagName").toString(), "");
		}

		tab3ImgName = StringUtils.defaultString(prop.get("tab3ImgName").toString());
		tab3ImgFolder = StringUtils.defaultString(prop.get("tab3ImgFolder").toString(), "");
		tab3ProdId = StringUtils.defaultString(prop.get("tab3ProdId").toString(), "");
		tab3Brief = StringUtils.defaultString(prop.get("tab3Brief").toString(), "");
		tab3Name = StringUtils.defaultString(prop.get("tab3Name").toString(), "");
		// 為了舊資料
		if (prop.containsKey("tab3TagType") && prop.containsKey("tab3TagName")) {
			tab3TagType = StringUtils.defaultString(prop.get("tab3TagType").toString(), "");
			tab3TagName = StringUtils.defaultString(prop.get("tab3TagName").toString(), "");
		}

		tab4ImgName = StringUtils.defaultString(prop.get("tab4ImgName").toString());
		tab4ImgFolder = StringUtils.defaultString(prop.get("tab4ImgFolder").toString(), "");
		tab4ProdId = StringUtils.defaultString(prop.get("tab4ProdId").toString(), "");
		tab4Brief = StringUtils.defaultString(prop.get("tab4Brief").toString(), "");
		tab4Name = StringUtils.defaultString(prop.get("tab4Name").toString(), "");
		// 為了舊資料
		if (prop.containsKey("tab4TagType") && prop.containsKey("tab4TagName")) {
			tab4TagType = StringUtils.defaultString(prop.get("tab4TagType").toString(), "");
			tab4TagName = StringUtils.defaultString(prop.get("tab4TagName").toString(), "");
		}

		// tab5ImgName = StringUtils.defaultString(prop.get("tab5ImgName").toString());
		// tab5ImgFolder = StringUtils.defaultString(prop.get("tab5ImgFolder").toString(), "");
		// tab5ProdId = StringUtils.defaultString(prop.get("tab5ProdId").toString(), "");
		// tab5Brief = StringUtils.defaultString(prop.get("tab5Brief").toString(), "");
		// tab5Name = StringUtils.defaultString(prop.get("tab5Name").toString(), "");
		// 為了舊資料
		// if (prop.containsKey("tab5TagType") && prop.containsKey("tab5TagName")) {
		// tab5TagType = StringUtils.defaultString(prop.get("tab5TagType").toString(), "");
		// tab5TagName = StringUtils.defaultString(prop.get("tab5TagName").toString(), "");
		// }
	}

	/**
	 * 储存广告
	 * 
	 * @return
	 * @throws Exception
	 * @throws IOException
	 * @throws ParseException
	 * @throws CoreException
	 * @throws NumberFormatException
	 */
	public String saveHotSelectAdv() throws NumberFormatException, CoreException, ParseException, IOException, Exception {
		double imgSize = 30;
		int imgWidth = 170;
		int imgHeight = 225;
		int error_image = 0;
		if (!hotSelectAdvValidate()) {
			return editHotSelectAdv();
		}

		contents = new StringBuilder("");
		// upload file
		if (GenerateHelper.mkDir(advType)) {
			if (StringUtils.isBlank(advId)) {// add
				if (validateFile(tab1Img, "tab1Img", "商品1图片", tab1ImgName, imgSize, imgWidth, imgHeight)) {
					contents.append(saveImgFile(tab1Img, tab1ImgName, advType, "tab1ImgName", "tab1ImgFolder"));
				} else {
					contents.append("tab1ImgName=").append(lineBreak);
					contents.append("tab1ImgFolder=").append(lineBreak);
					error_image++;
				}
				if (validateFile(tab2Img, "tab2Img", "商品2图片", tab2ImgName, imgSize, imgWidth, imgHeight)) {
					contents.append(saveImgFile(tab2Img, tab2ImgName, advType, "tab2ImgName", "tab2ImgFolder"));
				} else {
					contents.append("tab2ImgName=").append(lineBreak);
					contents.append("tab2ImgFolder=").append(lineBreak);
					error_image++;
				}
				if (validateFile(tab3Img, "tab3Img", "商品3图片", tab3ImgName, imgSize, imgWidth, imgHeight)) {
					contents.append(saveImgFile(tab3Img, tab3ImgName, advType, "tab3ImgName", "tab3ImgFolder"));
				} else {
					contents.append("tab3ImgName=").append(lineBreak);
					contents.append("tab3ImgFolder=").append(lineBreak);
					error_image++;
				}
				if (validateFile(tab4Img, "tab4Img", "商品4图片", tab4ImgName, imgSize, imgWidth, imgHeight)) {
					contents.append(saveImgFile(tab4Img, tab4ImgName, advType, "tab4ImgName", "tab4ImgFolder"));
				} else {
					contents.append("tab4ImgName=").append(lineBreak);
					contents.append("tab4ImgFolder=").append(lineBreak);
					error_image++;
				}
				// if (validateFile(tab5Img, "tab5Img", "商品5图片", tab5ImgName, imgSize, imgWidth, imgHeight)) {
				// contents.append(saveImgFile(tab5Img, tab5ImgName, advType, "tab5ImgName", "tab5ImgFolder"));
				// } else {
				// contents.append("tab5ImgName=").append(lineBreak);
				// contents.append("tab5ImgFolder=").append(lineBreak);
				// error_image++;
				// }
			} else {// update
				if (null == tab1Img) {
					contents.append("tab1ImgName=").append(tab1ImgName).append(lineBreak);
					contents.append("tab1ImgFolder=").append(tab1ImgFolder).append(lineBreak);
				} else {
					if (validateFile(tab1Img, "tab1Img", "商品1图片", tab1ImgName, imgSize, imgWidth, imgHeight)) {
						contents.append(saveImgFile(tab1Img, tab1ImgName, advType, "tab1ImgName", "tab1ImgFolder"));
					} else {
						contents.append("tab1ImgName=").append(lineBreak);
						contents.append("tab1ImgFolder=").append(lineBreak);
						error_image++;
					}
				}
				if (null == tab2Img) {
					contents.append("tab2ImgName=").append(tab2ImgName).append(lineBreak);
					contents.append("tab2ImgFolder=").append(tab2ImgFolder).append(lineBreak);
				} else {
					if (validateFile(tab2Img, "tab2Img", "商品2图片", tab2ImgName, imgSize, imgWidth, imgHeight)) {
						contents.append(saveImgFile(tab2Img, tab2ImgName, advType, "tab2ImgName", "tab2ImgFolder"));
					} else {
						contents.append("tab2ImgName=").append(lineBreak);
						contents.append("tab2ImgFolder=").append(lineBreak);
						error_image++;
					}
				}
				if (null == tab3Img) {
					contents.append("tab3ImgName=").append(tab3ImgName).append(lineBreak);
					contents.append("tab3ImgFolder=").append(tab3ImgFolder).append(lineBreak);
				} else {
					if (validateFile(tab3Img, "tab3Img", "商品3图片", tab3ImgName, imgSize, imgWidth, imgHeight)) {
						contents.append(saveImgFile(tab3Img, tab3ImgName, advType, "tab3ImgName", "tab3ImgFolder"));
					} else {
						contents.append("tab3ImgName=").append(lineBreak);
						contents.append("tab3ImgFolder=").append(lineBreak);
						error_image++;
					}
				}
				if (null == tab4Img) {
					contents.append("tab4ImgName=").append(tab4ImgName).append(lineBreak);
					contents.append("tab4ImgFolder=").append(tab4ImgFolder).append(lineBreak);
				} else {
					if (validateFile(tab4Img, "tab4Img", "商品4图片", tab4ImgName, imgSize, imgWidth, imgHeight)) {
						contents.append(saveImgFile(tab4Img, tab4ImgName, advType, "tab4ImgName", "tab4ImgFolder"));
					} else {
						contents.append("tab4ImgName=").append(lineBreak);
						contents.append("tab4ImgFolder=").append(lineBreak);
						error_image++;
					}
				}
				// if (null == tab5Img) {
				// contents.append("tab5ImgName=").append(tab5ImgName).append(lineBreak);
				// contents.append("tab5ImgFolder=").append(tab5ImgFolder).append(lineBreak);
				// } else {
				// if (validateFile(tab5Img, "tab5Img", "商品5图片", tab5ImgName, imgSize, imgWidth, imgHeight)) {
				// contents.append(saveImgFile(tab5Img, tab5ImgName, advType, "tab5ImgName", "tab5ImgFolder"));
				// } else {
				// contents.append("tab5ImgName=").append(lineBreak);
				// contents.append("tab5ImgFolder=").append(lineBreak);
				// error_image++;
				// }
				// }
			}
			contents.append("tab1ProdId=").append(tab1ProdId).append("\n");
			contents.append("tab1Brief=").append(tab1Brief).append("\n");
			contents.append("tab1Name=").append(tab1Name).append("\n");
			contents.append("tab1TagType=").append(tab1TagType).append("\n");
			contents.append("tab1TagName=").append(tab1TagName).append("\n");

			contents.append("tab2ProdId=").append(tab2ProdId).append("\n");
			contents.append("tab2Brief=").append(tab2Brief).append("\n");
			contents.append("tab2Name=").append(tab2Name).append("\n");
			contents.append("tab2TagType=").append(tab2TagType).append("\n");
			contents.append("tab2TagName=").append(tab2TagName).append("\n");

			contents.append("tab3ProdId=").append(tab3ProdId).append("\n");
			contents.append("tab3Brief=").append(tab3Brief).append("\n");
			contents.append("tab3Name=").append(tab3Name).append("\n");
			contents.append("tab3TagType=").append(tab3TagType).append("\n");
			contents.append("tab3TagName=").append(tab3TagName).append("\n");

			contents.append("tab4ProdId=").append(tab4ProdId).append("\n");
			contents.append("tab4Brief=").append(tab4Brief).append("\n");
			contents.append("tab4Name=").append(tab4Name).append("\n");
			contents.append("tab4TagType=").append(tab4TagType).append("\n");
			contents.append("tab4TagName=").append(tab4TagName).append("\n");

			// contents.append("tab5ProdId=").append(tab5ProdId).append("\n");
			// contents.append("tab5Brief=").append(tab5Brief).append("\n");
			// contents.append("tab5Name=").append(tab5Name).append("\n");
			// contents.append("tab5TagType=").append(tab5TagType).append("\n");
			// contents.append("tab5TagName=").append(tab5TagName).append("\n");

			if (StringUtils.isBlank(advId)) {
				advertise = new GenerateAdvertise();
				position = new GeneratePosition();
				content = new GenerateContent();
			} else {
				advertise = generateService.get(Long.parseLong(advId));
				position = generateService.getGeneratePositionByAdvId(Long.parseLong(advId)).get(0);
				content = generateService.getGenerateContentByAdvId(Long.parseLong(advId)).get(0);
			}
			// set advertise
			advertise.setAdvType(advType);
			advertise.setAdvName(advName.trim());
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			advertise.setStartDate(dateFormat.parse(startDate + " " + startTime + ":00"));
			advertise.setEndDate(dateFormat.parse(endDate + " " + endTime + ":59"));
			if (error_image > 0)
				advertise.setAdvStatus("90");
			else
				advertise.setAdvStatus(advStatus);
			position.setMainCateId(Long.parseLong(mainCateId));
			// logger.info("save content : {}", contents.toString());
			content.setContents(contents.toString());
			if (StringUtils.isBlank(advId))
				generateService.saveGenerateCombine(advertise, position, content);
			else
				generateService.updateGenerateCombine(advertise, position, content);

			contents.setLength(0);

			// 存檔後立刻執行產生檔案的邏輯
			generateService.generateHotSelectV2(Long.valueOf(mainCateId));
		}

		advId = advertise.getId().toString();
		addActionMessage("储存成功");

		return editHotSelectAdv();
	}

	EventImage saveImgFile(File imgFile, String fileName, String advType) throws Exception {
		try {
			eventImage = new EventImage();
			eventImage.setImgType(fileName.substring(fileName.lastIndexOf("."), fileName.length()));
			eventImage = generateService.saveEventImage(eventImage);
			boolean result = GenerateHelper.uploadImage(eventImage, imgFile, advType);
			if (result)
				return eventImage;
			else
				return null;
		} catch (Exception e) {
			logger.info("exception :{}", e.getMessage());
		}
		return null;
	}

	String saveImgFile(File imgFile, String fileName, String advType, String imgName, String imgFolder) throws Exception {
		StringBuffer sb = new StringBuffer();
		try {
			String today = FastDateFormat.getInstance(EcConstant.DEFAULT_PATTERN_DATE).format(DateUtil.getToday().getTime());
			eventImage = new EventImage();
			eventImage.setImgType(fileName.substring(fileName.lastIndexOf("."), fileName.length()));
			eventImage = generateService.saveEventImage(eventImage);
			boolean result = GenerateHelper.uploadImage(eventImage, imgFile, advType);
			if (result) {
				sb.append(imgName + "=").append(eventImage.getUuid()).append(eventImage.getImgType()).append(lineBreak);
				sb.append(imgFolder + "=").append(today).append(lineBreak);
			} else {
				sb.append(imgName + "=").append(lineBreak);
				sb.append(imgFolder + "=").append(lineBreak);
			}
		} catch (Exception e) {
			logger.info("exception :{}", e.getMessage());
		}
		return sb.toString();
	}

	/**
	 * @param advId the advId to set
	 */
	public void setAdvId(String advId) {
		this.advId = advId;
	}

	/**
	 * @param advName the advName to set
	 */
	public void setAdvName(String advName) {
		this.advName = advName;
	}

	/**
	 * @param advStatus the advStatus to set
	 */
	public void setAdvStatus(String advStatus) {
		this.advStatus = advStatus;
	}

	/**
	 * @param advType the advType to set
	 */
	public void setAdvType(String advType) {
		this.advType = advType;
	}

	public void setCategoryRoot(List<Category> categoryRoot) {
		this.categoryRoot = categoryRoot;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	/**
	 * @param content the content to set
	 */
	public void setContent(GenerateContent content) {
		this.content = content;
	}

	/**
	 * @param endDate the endDate to set
	 */
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}

	public void setModifyDate(Date modifyDate) {
		this.modifyDate = modifyDate;
	}

	public void setModifyUser(String modifyUser) {
		this.modifyUser = modifyUser;
	}

	public void setError(String error) {
		this.error = error;
	}

	/**
	 * @param generateService the generateService to set
	 */
	public void setGenerateService(GenerateService generateService) {
		this.generateService = generateService;
	}

	public void setMainCateId(String mainCateId) {
		this.mainCateId = mainCateId;
	}

	public void setPosition(GeneratePosition position) {
		this.position = position;
	}

	/**
	 * @param startDate the startDate to set
	 */
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public void setTab1Brief(String tab1Brief) {
		this.tab1Brief = tab1Brief;
	}

	/**
	 * @param tab1Img the tab1Img to set
	 */
	public void setTab1Img(File tab1Img) {
		this.tab1Img = tab1Img;
	}

	/**
	 * @param tab1ImgFolder the tab1ImgFolder to set
	 */
	public void setTab1ImgFolder(String tab1ImgFolder) {
		this.tab1ImgFolder = tab1ImgFolder;
	}

	/**
	 * @param tab1ImgName the tab1ImgName to set
	 */
	public void setTab1ImgName(String tab1ImgName) {
		this.tab1ImgName = tab1ImgName;
	}

	public void setTab1Name(String tab1Name) {
		this.tab1Name = tab1Name;
	}

	public void setTab1ProdId(String tab1ProdId) {
		this.tab1ProdId = tab1ProdId;
	}

	public void setTab2Brief(String tab2Brief) {
		this.tab2Brief = tab2Brief;
	}

	/**
	 * @param tab2Img the tab2Img to set
	 */
	public void setTab2Img(File tab2Img) {
		this.tab2Img = tab2Img;
	}

	/**
	 * @param tab2ImgFolder the tab2ImgFolder to set
	 */
	public void setTab2ImgFolder(String tab2ImgFolder) {
		this.tab2ImgFolder = tab2ImgFolder;
	}

	/**
	 * @param tab2ImgName the tab2ImgName to set
	 */
	public void setTab2ImgName(String tab2ImgName) {
		this.tab2ImgName = tab2ImgName;
	}

	public void setTab2Name(String tab2Name) {
		this.tab2Name = tab2Name;
	}

	public void setTab2ProdId(String tab2ProdId) {
		this.tab2ProdId = tab2ProdId;
	}

	public void setTab3Brief(String tab3Brief) {
		this.tab3Brief = tab3Brief;
	}

	/**
	 * @param tab3Img the tab3Img to set
	 */
	public void setTab3Img(File tab3Img) {
		this.tab3Img = tab3Img;
	}

	/**
	 * @param tab3ImgFolder the tab3ImgFolder to set
	 */
	public void setTab3ImgFolder(String tab3ImgFolder) {
		this.tab3ImgFolder = tab3ImgFolder;
	}

	/**
	 * @param tab3ImgName the tab3ImgName to set
	 */
	public void setTab3ImgName(String tab3ImgName) {
		this.tab3ImgName = tab3ImgName;
	}

	public void setTab3Name(String tab3Name) {
		this.tab3Name = tab3Name;
	}

	public void setTab3ProdId(String tab3ProdId) {
		this.tab3ProdId = tab3ProdId;
	}

	public void setTab4Brief(String tab4Brief) {
		this.tab4Brief = tab4Brief;
	}

	/**
	 * @param tab4Img the tab4Img to set
	 */
	public void setTab4Img(File tab4Img) {
		this.tab4Img = tab4Img;
	}

	/**
	 * @param tab4ImgFolder the tab4ImgFolder to set
	 */
	public void setTab4ImgFolder(String tab4ImgFolder) {
		this.tab4ImgFolder = tab4ImgFolder;
	}

	/**
	 * @param tab4ImgName the tab4ImgName to set
	 */
	public void setTab4ImgName(String tab4ImgName) {
		this.tab4ImgName = tab4ImgName;
	}

	public void setTab4Name(String tab4Name) {
		this.tab4Name = tab4Name;
	}

	public void setTab4ProdId(String tab4ProdId) {
		this.tab4ProdId = tab4ProdId;
	}

	// public void setTab5Brief(String tab5Brief) {
	// this.tab5Brief = tab5Brief;
	// }
	//
	// /**
	// * @param tab5Img the tab5Img to set
	// */
	// public void setTab5Img(File tab5Img) {
	// this.tab5Img = tab5Img;
	// }
	//
	// /**
	// * @param tab5ImgFolder the tab5ImgFolder to set
	// */
	// public void setTab5ImgFolder(String tab5ImgFolder) {
	// this.tab5ImgFolder = tab5ImgFolder;
	// }

	// /**
	// * @param tab5ImgName the tab5ImgName to set
	// */
	// public void setTab5ImgName(String tab5ImgName) {
	// this.tab5ImgName = tab5ImgName;
	// }

	// public void setTab5Name(String tab5Name) {
	// this.tab5Name = tab5Name;
	// }

	// public void setTab5ProdId(String tab5ProdId) {
	// this.tab5ProdId = tab5ProdId;
	// }

	/**
	 * @return the tab1TagType
	 */
	public String getTab1TagType() {
		return tab1TagType;
	}

	/**
	 * @param tab1TagType the tab1TagType to set
	 */
	public void setTab1TagType(String tab1TagType) {
		this.tab1TagType = tab1TagType;
	}

	/**
	 * @return the tab1TagName
	 */
	public String getTab1TagName() {
		return tab1TagName;
	}

	/**
	 * @param tab1TagName the tab1TagName to set
	 */
	public void setTab1TagName(String tab1TagName) {
		this.tab1TagName = tab1TagName;
	}

	/**
	 * @return the tab2TagType
	 */
	public String getTab2TagType() {
		return tab2TagType;
	}

	/**
	 * @param tab2TagType the tab2TagType to set
	 */
	public void setTab2TagType(String tab2TagType) {
		this.tab2TagType = tab2TagType;
	}

	/**
	 * @return the tab2TagName
	 */
	public String getTab2TagName() {
		return tab2TagName;
	}

	/**
	 * @param tab2TagName the tab2TagName to set
	 */
	public void setTab2TagName(String tab2TagName) {
		this.tab2TagName = tab2TagName;
	}

	/**
	 * @return the tab3TagType
	 */
	public String getTab3TagType() {
		return tab3TagType;
	}

	/**
	 * @param tab3TagType the tab3TagType to set
	 */
	public void setTab3TagType(String tab3TagType) {
		this.tab3TagType = tab3TagType;
	}

	/**
	 * @return the tab3TagName
	 */
	public String getTab3TagName() {
		return tab3TagName;
	}

	/**
	 * @param tab3TagName the tab3TagName to set
	 */
	public void setTab3TagName(String tab3TagName) {
		this.tab3TagName = tab3TagName;
	}

	/**
	 * @return the tab4TagType
	 */
	public String getTab4TagType() {
		return tab4TagType;
	}

	/**
	 * @param tab4TagType the tab4TagType to set
	 */
	public void setTab4TagType(String tab4TagType) {
		this.tab4TagType = tab4TagType;
	}

	/**
	 * @return the tab4TagName
	 */
	public String getTab4TagName() {
		return tab4TagName;
	}

	/**
	 * @param tab4TagName the tab4TagName to set
	 */
	public void setTab4TagName(String tab4TagName) {
		this.tab4TagName = tab4TagName;
	}

	// /**
	// * @return the tab5TagType
	// */
	// public String getTab5TagType() {
	// return tab5TagType;
	// }
	//
	// /**
	// * @param tab5TagType the tab5TagType to set
	// */
	// public void setTab5TagType(String tab5TagType) {
	// this.tab5TagType = tab5TagType;
	// }
	//
	// /**
	// * @return the tab5TagName
	// */
	// public String getTab5TagName() {
	// return tab5TagName;
	// }
	//
	// /**
	// * @param tab5TagName the tab5TagName to set
	// */
	// public void setTab5TagName(String tab5TagName) {
	// this.tab5TagName = tab5TagName;
	// }

	boolean validateFile(File imgFile, String fieldName, String fieldNameMsg, String fileName, double fileSize,
			int fileWidth, int fileHeight) throws IOException {
		int error_size = 0;
		if (null != imgFile) {
			if ((imgFile.length() / 1024) > fileSize) {
				addFieldError(fieldName, fieldNameMsg + "檔案尺寸不符");
				error_size++;
			}
			// flash檔不檢查長寬
			if (!StringUtils.endsWithIgnoreCase(fileName, ".swf")) {
				Image image = ImageIO.read(imgFile);
				if (image.getHeight(null) > fileHeight || image.getWidth(null) > fileWidth) {
					addFieldError(fieldName, fieldNameMsg + "檔案长度/宽度不符");
					error_size++;
				}
			}
			if (error_size > 0)
				return false;
			else
				return true;
		} else
			return false;
	}
}
